package com.example.bookManagement.Controller;
import com.example.bookManagement.Mapper.BookInfoMapper;
import com.example.bookManagement.Model.*;
import com.example.bookManagement.Service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;;

@RestController
@Slf4j
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo){
        log.info("添加图书：{}",bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName())||!StringUtils.hasLength(bookInfo.getAuthor())
        ||bookInfo.getCount()==null||bookInfo.getPrice()==null||!StringUtils.hasLength(bookInfo.getPublish())||
        bookInfo.getStatus()==null){
            return "输入不合法！";
        }
        try {
            Integer result = bookService.insertBook(bookInfo);
            if (result >0)
                return "";
        }catch (Exception e){
            log.info("添加图书失败",e);
            return e.getMessage();
        }
        return "添加失败！";
    }
    @RequestMapping("/getListByPage")
    public Result getListByPage(PageRequest pageRequest ,HttpSession session) {

//        //判断用户是否登录
//        if(session.getAttribute(Constants.SESSION_USER_KEY)==null){
//            return Result.unlogin();
//        }
//        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
//        if(userInfo==null||userInfo.getId()<0||"".equals(userInfo.getUserName())){
//            return Result.unlogin();
//        }
        PageResult<BookInfo> pageResult = bookService.getBookListByPage(pageRequest);
        log.info("获取图书列表,pageRequest:{}",pageRequest);
        System.out.println(Result.success(pageResult));
        return Result.success(pageResult);
    }
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        if(bookId==null||bookId<=0){
            return new BookInfo();
        }
        return bookService.queryBookById(bookId);
    }
    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        log.info("修改图书{}",bookInfo);
        try{
            bookService.updateBook(bookInfo);
            return "";
        }catch (Exception e){
            log.error("修改图书失败",e);
            return e.getMessage();
        }
    }
    @RequestMapping("/batchDeleteBook")
    public Boolean batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书{}",ids);
        try{
            bookService.batchDeleteBook(ids);
        } catch (Exception e) {
            log.info("批量删除失败！",e);
            return false;
        }
        return true;
    }
}
